/**
 * 参数处理
 * @param {*} params  参数
 */
export function tansParams(params: Record<string, any>) {
  let result = "";
  for (const propName of Object.keys(params)) {
    const value = params[propName];
    const part = encodeURIComponent(propName) + "=";
    if (value !== null && value !== "" && typeof value !== "undefined") {
      if (typeof value === "object") {
        for (const key of Object.keys(value)) {
          if (
            value[key] !== null &&
            value[key] !== "" &&
            typeof value[key] !== "undefined"
          ) {
            const params = propName + "[" + key + "]";
            const subPart = encodeURIComponent(params) + "=";
            result += subPart + encodeURIComponent(value[key]) + "&";
          }
        }
      } else {
        result += part + encodeURIComponent(value) + "&";
      }
    }
  }
  return result;
}

// 验证是否为blob格式
export function blobValidate(data: any) {
  return data.type !== "application/json";
}

export function formatDateToYMD(isoString: string) {
  const date = new Date(isoString);

  // 检查date是否有效

  if (isNaN(date.getTime())) {
    throw new Error("Invalid date string");
  }

  // 使用UTC方法获取年月日，因为输入是UTC时间，我们想得到确切的UTC日期

  const year = date.getUTCFullYear();

  const month = date.getUTCMonth() + 1; // 月份从0开始，所以+1

  const day = date.getUTCDate();

  return `${year}-${month.toString().padStart(2, "0")}-${day
    .toString()
    .padStart(2, "0")}`;
}
